<?php
class DaoModel extends CI_Model {
	//TODO - Insert your code here
	/**
	 *
	 * @access  public
	 * Constructor
	 */
	public  function __construct() {
		parent::__construct();
		//TODO - Insert your code here
	}
	/**
	 * 
	 * Get an object ID for this object 
	 * @param unknown_type $class
	 */
	public function GetOID($class){
		$this->db->insert('dao_data',array('type'=>$class) );
		return $this->db->insert_id();
	}
	
	public function LoadAll( $oids ){
		$retArray = array();
		$this->db->trans_start();
		$this->db->select('*');
		$this->db->from('dao_data');
		$this->db->where( "oid IN ($oids)" );
		$query = $this->db->get();
		$res = $query->result();
		$qDat = array();
		// pack each one into correct type
		foreach( $res as $obj ){
			if( !ISSET($qDat[ $obj->type ])){
				$qDat[ $obj->type ]=array();
			}
			$qDat[ $obj->type ] []= $obj->oid;
		}
				// now load each
		foreach ( $qDat as $key => $val ){
					$this->db->select('*');
					$this->db->from( $key );
					$this->db->where( "oid IN (".implode( "," ,$val ).")" );
					$query = $this->db->get();
					$res = $query->result();
					if( !ISSET($retArray[ $key ])){
							$retArray[ $key ]=array();
						}
					$retArray[ $key ] = $res;			
		}
		$this->db->trans_complete();
		return $retArray;
	}
	/***
	 * Create a table for this DAO
	 */
	public function CreateDAO( $name, $props ){
		$controller = CI_Controller::get_instance();
		$controller->load->dbforge();
		$controller->dbforge->add_field( $props );
		$controller->dbforge->add_key('id', TRUE);
		$controller->dbforge->create_table($name, TRUE);
	}
	/**
	 * 
	 * Check if this table exists ...
	 * @param unknown_type $name
	 */
	public function TableExists( $name ){
		return $this->db->table_exists( strtolower( $name ) );
	}
	
	/**
	 * Searches the given table for this id
	 * @param unknown_type $class
	 * @param unknown_type $oid
	 */
	public function exists($class,$oid){
		$this->db->where( "oid", $oid );
		$this->db->from($class);
		if( $this->db->count_all_results() > 0){
			return TRUE;
		}
		return FALSE;
	}
	/**
	 * 
	 * Validate a given table to check if columns match definition
	 */
	public function ValidateTableTooMany( $table,$data ){
		$tdata = $this->db->field_data( $table );
		$notmatched = array();
		foreach( $data as $key => $val ){
			$matched = FALSE;
			foreach( $tdata as $compare ){
				if( $compare->name == $key  ) {
				$matched = TRUE;
				}
			}
			if( !$matched ){
				$notmatched[]= $key;
			}
		}
		if( count($notmatched)>0 )return $notmatched;
	}
	
	/**
	 * 
	 * Validate a given table to check if columns match definition
	 */
	public function ValidateTableTooFew( $table,$data ){
		$tdata = $this->db->field_data( $table );
		$notmatched = array();
		foreach( $tdata as $compare ){
			$matched = FALSE;
			foreach( $data as $key => $val ){
				if( $compare->name == $key ) {
				$matched = TRUE;
				}
			}
			if( !$matched ){
				$notmatched[]= $compare->name;
			}
		}
		if( count($notmatched)>0 )return $notmatched;
	}
	/**
	 * 
	 * When you need to add in columns
	 * @param unknown_type $fields
	 */
	public function AddFields( $table,$fields ){
		$controller = CI_Controller::get_instance();
		$controller->load->dbforge();
		$controller->dbforge->add_column( $table,$fields );
	}
	/**
	 * 
	 * Removes unwanted fields from the subjects
	 * @param unknown_type $table
	 * @param unknown_type $fields
	 */
	public function RemoveFields( $table, $fields ){
		$controller = CI_Controller::get_instance();
		$controller->load->dbforge();
		foreach( $fields as $remove ){
			$controller->dbforge->drop_column( $table,$remove );
		}
	}
	/**
	 * 
	 * Load the data for this table
	 * @param unknown_type $table
	 * @param unknown_type $id
	 */
	public function Load( $table,$oid ){
		$query = $this->db->get_where( $table, array( 'oid' => $oid ) );
		$res = $query->result();
		return $res; 
	}
	/**
	 * delete all objects with these ids
	 * @param int $ids
	 */
	public function delete( $tables,$ids ){
		if( is_array( $ids )){
			if( count( $ids > 1) ){
				$oids = implode(",",$ids);
			}
			else {
				$oids = $ids[0];
			}
		}
		else {
			$oids = $ids;
		}
		$this->db->where( "oid IN ($oids)" );
		$tables[]="dao_data";
		$this->db->delete($tables);
	}
	/**
	 * 
	 * Load an object from just the oid
	 * @param unknown_type $oid
	 */
	public function loadObject( $oid ){
		$query = $this->db->get_where( "dao_data", array( 'oid' => $oid ) );
		$res = $query->result();
		return $res;
	}
	
	/**
	 * 
	 * Load an object from search criteria
	 * @param unknown_type $oid
	 */
	public function searchObject( $class,$terms ){
		$query = $this->db->get_where( $class, $terms );
		return $query->result();
	}
	/**
	 * Save this object
	 */
	public function save( $table,$data ){
		return $this->db->insert($table, $data ); 
	}
	
	/**
	 * Update this object
	 */
	public function update( $table,$id,$data ){
		$this->db->where( 'oid', $id );
		return $this->db->update( $table,$data );
	}
}


?>